package com.alm.token.utils;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;

import com.alm.token.ConStant;
import com.alm.token.MyApp;

import java.util.Locale;

/**
 * Created by en on 2019/2/13.
 */

public class LanguageUtils {
    public static final String CHINESE_SIMPLE = "zh_CN";
    public static final String COOKIE_CHINESE = "lang=zh_CN";
    public static final String ENGLISH = "en_US";
    public static final String COOKIE_ENGLISH = "lang=en_US";
    public static final String KOREAN = "ko_KR";
    public static final String COOKIE_KOREAN = "lang=ko_KR";
    public static final String JAPANESE = "ja_JP";
    public static final String COOKIE_JAPANESE = "lang=ja_JP";
    public static final String FRANCE = "fr_FR";
    public static final String COOKIE_FRANCE = "lang=fr_FR";

    public static final String AUTO = "auto";
    private static final String TAG = "LanguageUtils";
    //public static final String[] LOCALES = Utils.getContext().getResources().getStringArray(R.array.locales);

    private LanguageUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    public static void setSystemDefaultLocale(Locale locale) {

    }

    public static boolean isSetValue(Context context) {
        Locale currentLocale = context.getResources().getConfiguration().locale;
        return currentLocale.equals(getSetLocale());
    }

    private static Locale getSetLocale() {
        String locale = SpUtil.getString(MyApp.context, ConStant.LANGAUE, ""+LanguageUtils.CHINESE_SIMPLE);
        if (locale.equals(LanguageUtils.AUTO)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                return Resources.getSystem().getConfiguration().getLocales().get(0);//解决了获取系统默认错误的问题
            } else {
                return Locale.getDefault();
            }
        }
        String[] array = locale.split("_");
        String language = array[0];
        if (array.length > 1) {
            String country = array[1];
            return new Locale(language, country);
        }
        return new Locale(language);
    }

    public static int getSetIndex() {
        String languageSet = SpUtil.getString(MyApp.context, ConStant.LANGAUE, ""+LanguageUtils.CHINESE_SIMPLE);
        int localeIndex = 0;
        switch (languageSet) {
            case LanguageUtils.AUTO:
                localeIndex = 0;
                break;
            case LanguageUtils.CHINESE_SIMPLE:
                localeIndex = 1;
                break;
            case LanguageUtils.ENGLISH:
                localeIndex = 2;
                break;
            case LanguageUtils.KOREAN:
                localeIndex = 3;
                break;
            case LanguageUtils.JAPANESE:
                localeIndex = 4;
                break;
            case LanguageUtils.FRANCE:
                localeIndex = 5;
                break;
        }
        return localeIndex;
    }

    public static Context wrapContext(Context context) {
//        LogUtil.e();
        Resources resources = context.getResources();
        Locale locale = LanguageUtils.getSetLocale();

        Configuration configuration = resources.getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration.setLocale(locale);
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            configuration.setLocales(localeList);
        } else {
            configuration.setLocale(locale);
        }
        return context.createConfigurationContext(configuration);
    }

    public static Context applyChange(Context context) {
        Resources res = context.getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();

        Locale locale = getSetLocale();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            conf.setLocale(locale);
            LocaleList localeList = new LocaleList(locale);
            LocaleList.setDefault(localeList);
            conf.setLocales(localeList);
        } else {
            conf.setLocale(locale);
        }
        res.updateConfiguration(conf, dm);
        return context;
    }
}

